package demo4;

import java.io.IOException;
import java.net.*;
import java.util.Scanner;

public class UdpechoClient {
    DatagramSocket socket=null;
    private String serverIP;
    private int serverPort;

    public UdpechoClient(String ip,int port) throws SocketException {
        socket=new DatagramSocket();
        serverIP=ip;
        serverPort=port;
    }

    public void start() throws IOException {
        Scanner scanner=new Scanner(System.in);
        String request=scanner.next();
        DatagramPacket req=new DatagramPacket(request.getBytes(),request.getBytes().length, InetAddress.getByName(serverIP),serverPort);
        socket.send(req);

        DatagramPacket resp=new DatagramPacket(new byte[4096],4096);
        socket.receive(resp);
        String response=new String(resp.getData(),0,resp.getLength());
        System.out.println(response);
    }

    public static void main(String[] args) throws IOException {
        UdpechoClient client=new UdpechoClient("127.0.0.1",60000);
        client.start();
    }

}
